package cn.edu.scau.cmi.ema.controller.rest;

import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.io.IOException;

import com.google.gson.Gson;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;


import cn.edu.scau.cmi.ema.domain.base.CmiPagination;
import cn.edu.scau.cmi.ema.service.base.CmiPsonService;
import cn.edu.scau.cmi.ema.controller.base.CmiEmaController;
import cn.edu.scau.cmi.ema.dao.*;
import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.service.*;

import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.WebDataBinder;

@Controller("FoodbatchRestController")
public class FoodbatchRestController extends CmiEmaController {

	/**
	 * Create a new Foodbatch entity
	 * 
	 */
	@RequestMapping(value = "/newFoodbatch", method = RequestMethod.POST)
	@ResponseBody
	public Object newFoodbatch(@RequestBody Foodbatch foodbatch) {
		foodbatchService.saveFoodbatch(foodbatch);
		return JSONObject.parse(cmiPsonService.toJsonString(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatch.getId()), 2));
	}

	@RequestMapping(value = "/restNewFoodbatch", method = RequestMethod.POST)
	@ResponseBody
	public Object restNewFoodbatch(@RequestBody String foodbatchJson) {
		Foodbatch foodbatch = new Gson().fromJson(foodbatchJson, Foodbatch.class);
		appendReferences(foodbatch);
		foodbatchService.saveFoodbatch(foodbatch);
		return JSONObject.parse(cmiPsonService.toJsonString(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatch.getId()), 2));
	}

	@RequestMapping(value = "/saveFoodbatch", method = RequestMethod.PUT)
	@ResponseBody
	public Foodbatch saveFoodbatch(@RequestBody Foodbatch foodbatch) {
		foodbatchService.saveFoodbatch(foodbatch);
		return foodbatchDAO.findFoodbatchByPrimaryKey(foodbatch.getId());
	}

	@RequestMapping(value = "/restSaveFoodbatch", method = RequestMethod.PUT)
	@ResponseBody
	public Object restSaveFoodbatch(@RequestBody String foodbatchJson) {
		Foodbatch foodbatch = new Gson().fromJson(foodbatchJson, Foodbatch.class);
		appendReferences(foodbatch);
		CmiPsonService cmiPsonService = new CmiPsonService();
		foodbatchService.saveFoodbatch(foodbatch);
		return JSONObject.parse(cmiPsonService.toJsonString(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatch.getId()), 1));
	}

	@RequestMapping(value = "/restDeleteFoodbatch/{foodbatchId}", method = RequestMethod.DELETE)
	@ResponseBody
	public void restDeleteFoodbatch(@PathVariable Integer foodbatchId) {
		Foodbatch foodbatch = foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId);
		foodbatchService.deleteFoodbatch(foodbatch);
	}

	@RequestMapping(value = "/restConfirmDeleteFoodbatchs")
	@ResponseBody
	public Object restConfirmDeleteFoodbatchs(@RequestBody Set<Integer> deleteFoodbatchsIds) {
		Set<Foodbatch> deleteFoodbatchs = foodbatchDAO.findFoodbatchsByIds(deleteFoodbatchsIds);
		Set<Foodbatch> referencedFoodbatchs = foodbatchService.loadReferenceFoodbatchs(deleteFoodbatchs);
		deleteFoodbatchs.removeAll(referencedFoodbatchs);
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("referencedEntitys", referencedFoodbatchs);
		jsonObject.put("deleteEntitys", deleteFoodbatchs);
		return JSONObject.parse(jsonObject.toJSONString());
	}

	@RequestMapping("/restDeleteFoodbatchs")
	@ResponseBody
	public Object restDeleteFoodbatchs(@RequestBody Set<Foodbatch> deleteFoodbatchs) {
		Iterator<Foodbatch> iterator = deleteFoodbatchs.iterator();
		while (iterator.hasNext()) {
			Foodbatch currentFoodbatch = iterator.next();
			foodbatchService.deleteFoodbatch(currentFoodbatch);
		}
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("url", "listFoodbatchs");
		return JSONObject.parse(jsonObject.toJSONString());
	}
	// loadself

	@RequestMapping(value = "/restLoadFoodbatch/{foodbatchId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadFoodbatch(@PathVariable Integer foodbatchId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject
				.parse(cmiPsonService.toPsonString(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId), 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadFoodbatchDetectmethod/{foodbatchId}/detectmethod", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadFoodbatchDetectmethod(@PathVariable Integer foodbatchId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService
				.toPsonString(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId).getDetectmethod(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadFoodbatchFood/{foodbatchId}/food", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadFoodbatchFood(@PathVariable Integer foodbatchId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(
				cmiPsonService.toPsonString(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId).getFood(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadFoodbatchUnit/{foodbatchId}/unit", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadFoodbatchUnit(@PathVariable Integer foodbatchId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(
				cmiPsonService.toPsonString(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId).getUnit(), 2, cmiPagination));
	}
	// loadReferenceds
	// TODO loadReferencedsPson，这个方法有一些问题，待修复

	@RequestMapping(value = "/restLoadFoodbatchDetects/{foodbatchId}/detects/{relateddetectId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadFoodbatchDetects(@PathVariable Integer foodbatchId, @PathVariable Integer relateddetectId,
			@RequestBody CmiPagination cmiPagination) {
		Detect detect = detectDAO.findDetectByPrimaryKey(relateddetectId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(detect, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadFoodbatchEvents/{foodbatchId}/events/{relatedeventId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadFoodbatchEvents(@PathVariable Integer foodbatchId, @PathVariable Integer relatedeventId,
			@RequestBody CmiPagination cmiPagination) {
		Event event = eventDAO.findEventByPrimaryKey(relatedeventId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(event, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadFoodbatchFoodbatchadditives/{foodbatchId}/foodbatchadditives/{relatedfoodbatchadditiveId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadFoodbatchFoodbatchadditives(@PathVariable Integer foodbatchId,
			@PathVariable Integer relatedfoodbatchadditiveId, @RequestBody CmiPagination cmiPagination) {
		Foodbatchadditive foodbatchadditive = foodbatchadditiveDAO
				.findFoodbatchadditiveByPrimaryKey(relatedfoodbatchadditiveId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(foodbatchadditive, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadFoodbatchFoodbatchfillers/{foodbatchId}/foodbatchfillers/{relatedfoodbatchfillerId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadFoodbatchFoodbatchfillers(@PathVariable Integer foodbatchId,
			@PathVariable Integer relatedfoodbatchfillerId, @RequestBody CmiPagination cmiPagination) {
		Foodbatchfiller foodbatchfiller = foodbatchfillerDAO.findFoodbatchfillerByPrimaryKey(relatedfoodbatchfillerId,
				-1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(foodbatchfiller, 2, cmiPagination));
	}

	@RequestMapping(value = "/restLoadFoodbatchFoodbatchmaterials/{foodbatchId}/foodbatchmaterials/{relatedfoodbatchmaterialId}", method = RequestMethod.POST)
	@ResponseBody
	public Object restLoadFoodbatchFoodbatchmaterials(@PathVariable Integer foodbatchId,
			@PathVariable Integer relatedfoodbatchmaterialId, @RequestBody CmiPagination cmiPagination) {
		Foodbatchmaterial foodbatchmaterial = foodbatchmaterialDAO
				.findFoodbatchmaterialByPrimaryKey(relatedfoodbatchmaterialId, -1, -1);
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(foodbatchmaterial, 2, cmiPagination));
	}

	public void appendReferences(Foodbatch foodbatch) {

		Detectmethod detectmethod = foodbatch.getDetectmethod();
		if (detectmethod != null)
			foodbatch.setDetectmethod(detectmethodService.findDetectmethodByPrimaryKey(detectmethod.getId()));

		Food food = foodbatch.getFood();
		if (food != null)
			foodbatch.setFood(foodService.findFoodByPrimaryKey(food.getId()));

		Unit unit = foodbatch.getUnit();
		if (unit != null)
			foodbatch.setUnit(unitService.findUnitByPrimaryKey(unit.getId()));

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Detect> detectsForFoodbatch = new HashSet<>();
		for (Detect detect_Foodbatch : foodbatch.getDetectsForFoodbatch()) {
			detectsForFoodbatch.add(detectService.findDetectByPrimaryKey(detect_Foodbatch.getId()));
		}
		foodbatch.setDetectsForFoodbatch(detectsForFoodbatch);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Event> eventsForFoodbatch = new HashSet<>();
		for (Event event_Foodbatch : foodbatch.getEventsForFoodbatch()) {
			eventsForFoodbatch.add(eventService.findEventByPrimaryKey(event_Foodbatch.getId()));
		}
		foodbatch.setEventsForFoodbatch(eventsForFoodbatch);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Foodbatchadditive> foodbatchadditivesForFoodbatch = new HashSet<>();
		for (Foodbatchadditive foodbatchadditive_Foodbatch : foodbatch.getFoodbatchadditivesForFoodbatch()) {
			foodbatchadditivesForFoodbatch.add(
					foodbatchadditiveService.findFoodbatchadditiveByPrimaryKey(foodbatchadditive_Foodbatch.getId()));
		}
		foodbatch.setFoodbatchadditivesForFoodbatch(foodbatchadditivesForFoodbatch);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Foodbatchfiller> foodbatchfillersForFoodbatch = new HashSet<>();
		for (Foodbatchfiller foodbatchfiller_Foodbatch : foodbatch.getFoodbatchfillersForFoodbatch()) {
			foodbatchfillersForFoodbatch
					.add(foodbatchfillerService.findFoodbatchfillerByPrimaryKey(foodbatchfiller_Foodbatch.getId()));
		}
		foodbatch.setFoodbatchfillersForFoodbatch(foodbatchfillersForFoodbatch);

		// 引用表不是纯联系表，引用数量大于或者等于2
		Set<Foodbatchmaterial> foodbatchmaterialsForFoodbatch = new HashSet<>();
		for (Foodbatchmaterial foodbatchmaterial_Foodbatch : foodbatch.getFoodbatchmaterialsForFoodbatch()) {
			foodbatchmaterialsForFoodbatch.add(
					foodbatchmaterialService.findFoodbatchmaterialByPrimaryKey(foodbatchmaterial_Foodbatch.getId()));
		}
		foodbatch.setFoodbatchmaterialsForFoodbatch(foodbatchmaterialsForFoodbatch);
	}

	@RequestMapping(value = "/restGetFoodbatchDetectmethod/{foodbatchId}/detectmethod", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetFoodbatchDetectmethod(@PathVariable Integer foodbatchId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService
				.toPsonString(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId).getDetectmethod(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetFoodbatchFood/{foodbatchId}/food", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetFoodbatchFood(@PathVariable Integer foodbatchId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(
				cmiPsonService.toPsonString(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId).getFood(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetFoodbatchUnit/{foodbatchId}/unit", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetFoodbatchUnit(@PathVariable Integer foodbatchId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(
				cmiPsonService.toPsonString(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId).getUnit(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetFoodbatchDetects/{foodbatchId}/detects", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetFoodbatchDetects(@PathVariable Integer foodbatchId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId).getDetectsForFoodbatch(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetFoodbatchEvents/{foodbatchId}/events", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetFoodbatchEvents(@PathVariable Integer foodbatchId, @RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService
				.toPsonString(foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId).getEventsForFoodbatch(), 2, cmiPagination));
	}

	@RequestMapping(value = "/restGetFoodbatchFoodbatchadditives/{foodbatchId}/foodbatchadditives", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetFoodbatchFoodbatchadditives(@PathVariable Integer foodbatchId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId).getFoodbatchadditivesForFoodbatch(), 2,
				cmiPagination));
	}

	@RequestMapping(value = "/restGetFoodbatchFoodbatchfillers/{foodbatchId}/foodbatchfillers", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetFoodbatchFoodbatchfillers(@PathVariable Integer foodbatchId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId).getFoodbatchfillersForFoodbatch(), 2,
				cmiPagination));
	}

	@RequestMapping(value = "/restGetFoodbatchFoodbatchmaterials/{foodbatchId}/foodbatchmaterials", method = RequestMethod.POST)
	@ResponseBody
	public Object restGetFoodbatchFoodbatchmaterials(@PathVariable Integer foodbatchId,
			@RequestBody CmiPagination cmiPagination) {
		CmiPsonService cmiPsonService = new CmiPsonService();
		return JSONObject.parse(cmiPsonService.toPsonString(
				foodbatchDAO.findFoodbatchByPrimaryKey(foodbatchId).getFoodbatchmaterialsForFoodbatch(), 2,
				cmiPagination));
	}

	@RequestMapping(value = "/restListFoodbatchs", method = RequestMethod.GET)
	@ResponseBody
	public Object restListFoodbatchs() {
		return JSONObject.parse(cmiPsonService.toJsonString(foodbatchService.loadFoodbatchs(), 2));
	}

	@RequestMapping(value = "/restListFoodbatchsPost", method = RequestMethod.POST)
	@ResponseBody
	public Object restListFoodbatchsPost(@RequestBody CmiPagination cmiPagination) {

		CmiPsonService cmiPsonService = new CmiPsonService();
		cmiPagination = foodbatchPaginationService.updateAppPagination(cmiPagination);
		int startResult = (cmiPagination.getCurrentPagination() - 1) * cmiPagination.getPaginationSize();

		Set<Foodbatch> foodbatchs = foodbatchDAO.findAllFoodbatchs(startResult, cmiPagination.getPaginationSize());
		String foodbatchString = cmiPsonService.toPsonString(foodbatchs, 2, cmiPagination);

		return JSONObject.parse(foodbatchString);
	}

	@RequestMapping("/foodbatchSelectBox")
	@ResponseBody
	public JSONObject foodbatchSelectBox(@RequestParam(required = false) String searchTable,
			@RequestParam(required = false) Set<Integer> searchValue, // 杨宏宇2019-9-10：searchValue是在selectpage.js的616行定义，如果要修改searchValue的名称，1.可以修改在selectpage.js修改；2.可以
																		// @RequestParam(value="searchValue", required =
																		// false) Set<Integer> someName
			@RequestParam(required = false, defaultValue = "10") int paginationSize,
			@RequestParam(required = false, defaultValue = "1") int pageNumber,
			@RequestParam(value = "qWord[]", required = false) String queryword,
			@RequestParam(required = false) String selectItem) throws IOException {
		Set entitySet = null;
		Integer count = 0;

		// 初始化原始值

		if (searchValue != null) {
			switch (searchTable) {
			// 外键
			case "Detectmethod":
				entitySet = detectmethodService.loadDetectmethodsByIdSet(searchValue);
				break;
			case "Food":
				entitySet = foodService.loadFoodsByIdSet(searchValue);
				break;
			case "Unit":
				entitySet = unitService.loadUnitsByIdSet(searchValue);
				break;

			// 被引用键
			case "Detect":
				entitySet = detectService.loadDetectsByIdSet(searchValue);
				break;
			case "Event":
				entitySet = eventService.loadEventsByIdSet(searchValue);
				break;
			case "Foodbatchadditive":
				entitySet = foodbatchadditiveService.loadFoodbatchadditivesByIdSet(searchValue);
				break;
			case "Foodbatchfiller":
				entitySet = foodbatchfillerService.loadFoodbatchfillersByIdSet(searchValue);
				break;
			case "Foodbatchmaterial":
				entitySet = foodbatchmaterialService.loadFoodbatchmaterialsByIdSet(searchValue);
				break;
			}
			// 没有测试，深度需要多少？？？
			String jsonData = cmiPsonService.toJsonString(entitySet, 1);
			JSONArray jsonArray = new JSONArray();
			jsonArray = (JSONArray) JSON.parse((String) jsonData);

			JSONObject jsonObject = new JSONObject();
			jsonObject.put("list", jsonArray);
			jsonObject.put("totalRow", entitySet.size());
			return jsonObject;
		}

		// 不根据关键字进行搜索
		if (queryword == null || queryword.trim() == "") {
			switch (selectItem) {
			// 外键
			case "Detectmethod":
				entitySet = detectmethodDAO.findAllDetectmethods((pageNumber - 1) * paginationSize, paginationSize);
				count = detectmethodService.countDetectmethods();
				break;
			case "Food":
				entitySet = foodDAO.findAllFoods((pageNumber - 1) * paginationSize, paginationSize);
				count = foodService.countFoods();
				break;
			case "Unit":
				entitySet = unitDAO.findAllUnits((pageNumber - 1) * paginationSize, paginationSize);
				count = unitService.countUnits();
				break;

			// 被引用键
			case "Detect":
				entitySet = detectDAO.findAllDetects((pageNumber - 1) * paginationSize, paginationSize);
				count = detectService.countDetects();
				break;
			case "Event":
				entitySet = eventDAO.findAllEvents((pageNumber - 1) * paginationSize, paginationSize);
				count = eventService.countEvents();
				break;
			case "Foodbatchadditive":
				entitySet = foodbatchadditiveDAO.findAllFoodbatchadditives((pageNumber - 1) * paginationSize,
						paginationSize);
				count = foodbatchadditiveService.countFoodbatchadditives();
				break;
			case "Foodbatchfiller":
				entitySet = foodbatchfillerDAO.findAllFoodbatchfillers((pageNumber - 1) * paginationSize,
						paginationSize);
				count = foodbatchfillerService.countFoodbatchfillers();
				break;
			case "Foodbatchmaterial":
				entitySet = foodbatchmaterialDAO.findAllFoodbatchmaterials((pageNumber - 1) * paginationSize,
						paginationSize);
				count = foodbatchmaterialService.countFoodbatchmaterials();
				break;
			}
		} else {
			switch (selectItem) {
			// 外键
			case "Detectmethod":
				entitySet = detectmethodDAO.findDetectmethodByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = detectmethodService.countDetectmethodsByName(queryword);
				break;
			case "Food":
				entitySet = foodDAO.findFoodByNameContaining(queryword, (pageNumber - 1) * paginationSize,
						paginationSize);
				count = foodService.countFoodsByName(queryword);
				break;
			case "Unit":
				entitySet = unitDAO.findUnitByNameContaining(queryword, (pageNumber - 1) * paginationSize,
						paginationSize);
				count = unitService.countUnitsByName(queryword);
				break;

			// 被引用键
			case "Detect":
				entitySet = detectDAO.findDetectByNameContaining(queryword, (pageNumber - 1) * paginationSize,
						paginationSize);
				count = detectService.countDetectsByName(queryword);
				break;
			case "Event":
				entitySet = eventDAO.findEventByNameContaining(queryword, (pageNumber - 1) * paginationSize,
						paginationSize);
				count = eventService.countEventsByName(queryword);
				break;
			case "Foodbatchadditive":
				entitySet = foodbatchadditiveDAO.findFoodbatchadditiveByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = foodbatchadditiveService.countFoodbatchadditivesByName(queryword);
				break;
			case "Foodbatchfiller":
				entitySet = foodbatchfillerDAO.findFoodbatchfillerByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = foodbatchfillerService.countFoodbatchfillersByName(queryword);
				break;
			case "Foodbatchmaterial":
				entitySet = foodbatchmaterialDAO.findFoodbatchmaterialByNameContaining(queryword,
						(pageNumber - 1) * paginationSize, paginationSize);
				count = foodbatchmaterialService.countFoodbatchmaterialsByName(queryword);
				break;
			}
		}
		/**
		 * 将Json数据转换成jsonArray形式，需要通过pson方式进行转换，然后再转换成JsonArray。因为存在懒加载
		 * 没有直接使用CmiPsonService中的fromJson方法是因为，fromJson方法会将int类型的直接转换成double类型的数据，这个问题还没有解决
		 */
		// 没有测试，深度需要多少？？？
		String jsonData = cmiPsonService.toJsonString(entitySet, 1);
		JSONArray jsonArray = new JSONArray();
		jsonArray = (JSONArray) JSON.parse((String) jsonData);

		JSONObject jsonObject = new JSONObject();
		jsonObject.put("list", jsonArray);
		jsonObject.put("paginationSize", paginationSize);
		jsonObject.put("pageNumber", pageNumber);
		jsonObject.put("totalRow", count);
		jsonObject.put("totalPaginations",
				count % paginationSize == 0 ? count / paginationSize : count / paginationSize + 1);
		return jsonObject;
	}
}
